$('#cid').on("focus", function () {
    if (!$('#alert').is(":hidden")) {
        $('#alert').hide().html("");
    }
})
$('#myButton').on("click", function () {

    var getUrl = $('#cid').val();

    if (getUrl == "" || getUrl == $('#cid')[0].defaultValue) {
        wranMsg("请输入抓取链接");
    } else {
         cid = getUrl.indexOf('=') > 0 ? getUrl.substr(getUrl.lastIndexOf('=') + 1) : getUrl.substr(getUrl.lastIndexOf('\/') + 1);
        var $btn = $(this).button('loading');
        $.ajax({
            url: "http://127.0.0.1:1337",
            type: "GET",
            dataType: "jsonp",
            jsonp: 'callback',
            jsonpCallback: 'getdata',
            data: {
                cid: cid,
                related_contest_count: "9",
                include_related_creator: "false",
                request_sizing_chart_info: "true",
                _buckets:"",
                _experiments: ""
            },
            success: function (data) {
                $btn.button('reset')
                if (data.code != 0) {
                    wranMsg(data.msg);
                } else {
                    showProductDetail(data.data.contest);
                }
            }
        })
    }

})

function showProductDetail(data) {
    $('.item .data').empty();
    $('#name .data').append('<div class="row"><div class="col-md-12"><div class="col-md-2"><div class="main_pic "><img class="" src="https://contestimg.wish.com/api/webimage/'+cid+'-0-original" alt=""></div></div><div class="col-md-10"><h4>' + data.name + '</h4></div></div></div>');
    var obj = {
        mspc: data.value,
        sell_price: data.commerce_product_info.variations[0].original_price,
        shipping: data.commerce_product_info.variations[0].original_shipping,
        wish_sell_price: data.commerce_product_info.variations[0].price_before_personal_price,
        wish_shipping: data.commerce_product_info.variations[0].shipping_before_personal_price,
}

    obj.wish_increase=-(obj.sell_price + obj.shipping - obj.wish_sell_price - obj.wish_shipping);
    obj.sesp=(obj.sell_price/obj.shipping).toFixed(2)+":1";
    obj.wish_sesp=(obj.wish_sell_price/obj.wish_shipping).toFixed(2)+":1";


    var txtArr = ["用户原价", "用户售价", "用户运费",  "WISH售价", "WISH运费","WISH提价", "用户售价运费比", "WISH售价运费比"];
    var extraPhoto_length = data.num_extra_photos;//图片长度
    var curr=0;
    for(var i in obj){
        $('#price .data').append('<div class="col-sm-4"><label class="control-label">'+txtArr[curr]+'：</label><span class="'+i+'">' + obj[i] + '</span></div>');
        curr++;
    }
    /*图片*/
    (function () {
        var html='<ul>';
        for(var i=0;i<=extraPhoto_length;i++){

            (function (i) {
                var Img=new Image(),
                    src="https://contestimg.wish.com/api/webimage/"+cid+"-"+i+"-original";
                Img.src=src;
                /***********提前获取图片的高度和宽度***********/
                /* if(Img.complete){//是否缓存读取
                 console.log("======com======");
                 console.log("img.width:"+i+":"+Img.width+"   img.height:"+Img.height);
                 }else{
                 Img.onload = function(e){//图片加载完毕
                 console.log("======load======");
                 console.log("img.width:"+i+":"+Img.width+"   img.height:"+Img.height);

                 }
                 }*/
                html+='<li class="pro_img_wrap"><div class="pro_img"><img src="'+src+'" alt=""></div><p class="pic_url"><input type="text" class="pic_url_ipt copy_input" value="'+src+'"></p></li>';


            })(i)
            /**********************/
        }
        html+="</ul>";
        $('#thumb .data').append(html);
    })();


    /*Tags*/
    $.each(data.tags, function (i, item) {
        $('#tags .data').append('<input class="col-sm-3 tag_input copy_input" value="' + item.name + '" />');
    })
    /*Merchant Tags*/
    $.each(data.merchant_tags, function (i, item) {
        $('#merchant_tags .data').append('<input class="col-sm-3 tag_input copy_input" value="' + item.name + '" />');
    })
    /*描述*/
    $('#description .data').append(data.description.replace(/\n/g, "<br>"));

}
function wranMsg(msg) {
    $('#alert').html(msg).fadeIn();
}

$('body').on("click",".copy_input", function () {
    $(this).select();
})

/*Tab*/
$('.tab_hd').on("click", function () {
    var idx=$(this).index();
    $(this).addClass("curr").siblings().removeClass('curr')
    $('.tab_bd').eq(idx).show().siblings().hide();
})